package Game.Objects;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

import GUI.GuiManager;
import Game.GameHelper;
import Game.Items.GameItem;
import Game.Items.Craft.GIApple;
import Game.Items.Craft.GIBone;
import Game.Items.Craft.GICarrot;
import Game.Items.Craft.GIChickenLeg;
import Game.Items.Craft.GIPotato;
import Game.Items.Craft.GIRatMeat;
import Game.Items.Craft.GISteelIngot;
import Game.Items.Others.GIChestKey;
import Game.Items.Others.GITorch;
import Game.Items.Potions.GIAntipoisonPotion;
import Game.Items.Potions.GIBigHealthPotion;
import Game.Items.Potions.GIMediumHealthPotion;
import Game.Items.Potions.GIPoisonPotion;
import Game.Items.Potions.GISmallHealthPotion;
import Game.Items.Potions.GIStrengthPotion;
import Game.Objects.Creatures.GOCreature;
import Tools.SoundManager;

/** Barrel class
 * @author Aikidos
 */
public class GOBarrel extends GameObject {

	/** List of item class (with drop chance) */
	private Map<GameItem, Integer> _lstItems = new HashMap<GameItem, Integer>();
	
	public GOBarrel(ObjectManager objectManager) throws SlickException {
		super(objectManager, "Barrel");
		
		_isCanBeUsed = true;
		
		setSprite(new Image("res/gfx/objects/barrel.png"));
		
		// Add items	
		_lstItems.put(new GISmallHealthPotion(), 20);
		_lstItems.put(new GIMediumHealthPotion(), 20);
		_lstItems.put(new GIBigHealthPotion(), 20);
		_lstItems.put(new GIAntipoisonPotion(), 20);
		_lstItems.put(new GIStrengthPotion(), 10);
		_lstItems.put(new GIMediumHealthPotion(), 10);
		_lstItems.put(new GIPoisonPotion(), 10);
		
		_lstItems.put(new GIRatMeat(), 50);
		_lstItems.put(new GIBone(), 50);
		_lstItems.put(new GISteelIngot(), 10);
		_lstItems.put(new GITorch(), 20);
		_lstItems.put(new GIChestKey(), 10);
		_lstItems.put(new GIChickenLeg(), 50);
		_lstItems.put(new GIApple(), 50);
		_lstItems.put(new GICarrot(), 50);
		_lstItems.put(new GIPotato(), 50);
	}

	@Override
	public void use(GameObject user) throws SlickException,
			InstantiationException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException,
			NoSuchMethodException, SecurityException, ClassNotFoundException {
		
		// Player
		if (user.isPlayer()) {
			GOPlayer player = (GOPlayer) user;
			
			boolean isEmpty = true;
			for (Entry<GameItem, Integer> entry : _lstItems.entrySet()) {
				if (GameHelper.getRandom().nextInt(1000 - player.getLuck()) <= entry.getValue()) {
					isEmpty = false;
					
					player.getInventory().addItem(entry.getKey());
					
					// Show message
					GuiManager.getMessenger().addMessage("[green]You[/green] picked up a [yellow]" 
							+ entry.getKey().getName() + "[/yellow]");
				}
			}
			
			// If chest is empty
			if (isEmpty) {
				// Show message
				GuiManager.getMessenger().addMessage("[yellow]" + getName() + "[/yellow] is empty");
			}
			
			// Play sound
			SoundManager.play("sfx_barrel", 0.1f);
		}
		
		// Creature
		if (user.isCreature()) {
			GOCreature creature = (GOCreature) user;
			
			for (Entry<GameItem, Integer> entry : _lstItems.entrySet()) {
				if (GameHelper.getRandom().nextInt(1000) <= entry.getValue()) {
					creature.getInventory().addItem(entry.getKey());
				}
			}
		}
		
		_objectManager.destroyObject(this);
		
	}

}
